#include "SystemSDL_1_2.h"

#include <SDL.h>
#include <cstdarg>
#include <cstdio>

#define PRINT_ENUM( x )                 \
    case x:                             \
    {                                   \
        Printf( #x );                   \
        Printf( "\t" );                 \
        break;                          \
    }

SDL_RWops* CSystemSDL_1_2_Helper::m_pDataFile = NULL;
unsigned int CSystemSDL_1_2_Helper::m_DebugPrint = PF_CONSOLE;

int CSystemSDL_1_2_Helper::Init( void )
{
    m_pDataFile = SDL_RWFromFile( "data.txt", "wt" );

    if( m_pDataFile == NULL )
        printf( "Could not create data file: %s", SDL_GetError() );

    return 0;
}

void CSystemSDL_1_2_Helper::ShutDown( void )
{
    if( m_pDataFile != NULL )
    {
        SDL_RWclose( m_pDataFile );
        m_pDataFile = NULL;
    }
}

void CSystemSDL_1_2_Helper::Printf( const char* data, ... )
{
    char msg[1026];
    va_list ptr;

    va_start( ptr, data );
        vsnprintf( msg, 1026, data, ptr );
    va_end( ptr );

    switch( m_DebugPrint )
    {
    case PF_CONSOLE:
        {
            printf( "%s", msg );
            break;
        }
    case PF_FILE:
        {
            SDL_RWwrite( m_pDataFile, msg, 1, SDL_strlen( msg ) );
            break;
        }
    case PF_ALL:
        {
            printf( "%s", msg );
            SDL_RWwrite( m_pDataFile, msg, 1, SDL_strlen( msg ) );
            break;
        }
    default: break;
    }
}

void CSystemSDL_1_2_Helper::PrintEvent( const SDL_Event& ev )
{
    //Printf( "Event detected: " );
    PrintFlag( ev.type );

    switch( ev.type )
    {
    case SDL_ACTIVEEVENT:
        {
            if( ev.active.gain )
                Printf( " focus is gained\n");
            else
                Printf( " focus is lost\n" );
            break;
        }
    case SDL_VIDEOEXPOSE:
        {
            Printf( "\n" );
            break;
        }
    case SDL_VIDEORESIZE:
        {
            Printf( " to %dx%d\n" );
            break;
        }
    case SDL_KEYDOWN:
    case SDL_KEYUP:
        {
            Printf( "%s\t", SDL_GetKeyName( ev.key.keysym.sym ) );
            if( ev.key.state )
                Printf( " is down\n" );
            else
                Printf( " is up\n" );
            break;
        }
    case SDL_MOUSEBUTTONDOWN:
    case SDL_MOUSEBUTTONUP:
        {
            Printf( " " );
            PrintMouseFlag( ev.button.button );

            if( ev.button.state )
                Printf( " is down" );
            else
                Printf( " is up" );

            Printf( " @ %dx%d\n", ev.button.x, ev.button.y );
            break;
        }
    default: break;
    }
}

void CSystemSDL_1_2_Helper::PrintFlag( unsigned int flag )
{
    switch( flag )
    {
        PRINT_ENUM( SDL_ACTIVEEVENT )
        PRINT_ENUM( SDL_KEYUP )
        PRINT_ENUM( SDL_KEYDOWN )
        PRINT_ENUM( SDL_MOUSEBUTTONDOWN )
        PRINT_ENUM( SDL_MOUSEBUTTONUP )
        PRINT_ENUM( SDL_JOYAXISMOTION )
        PRINT_ENUM( SDL_JOYBALLMOTION )
        PRINT_ENUM( SDL_JOYHATMOTION )
        PRINT_ENUM( SDL_JOYBUTTONDOWN )
        PRINT_ENUM( SDL_JOYBUTTONUP )
        PRINT_ENUM( SDL_VIDEOEXPOSE )
        PRINT_ENUM( SDL_VIDEORESIZE )
        PRINT_ENUM( SDL_QUIT )
        PRINT_ENUM( SDL_USEREVENT )
        PRINT_ENUM( SDL_SYSWMEVENT )

        default: break;
    }
}

void CSystemSDL_1_2_Helper::PrintMouseFlag( unsigned int flag )
{
    switch( flag )
    {
        PRINT_ENUM( SDL_BUTTON_LEFT );
        PRINT_ENUM( SDL_BUTTON_MIDDLE);
        PRINT_ENUM( SDL_BUTTON_RIGHT );
        PRINT_ENUM( SDL_BUTTON_WHEELUP );
        PRINT_ENUM( SDL_BUTTON_WHEELDOWN );
        PRINT_ENUM( SDL_BUTTON_X1 );
        PRINT_ENUM( SDL_BUTTON_X2 );

        default: break;
    }
}
